package com.greenantmall.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.List;

import com.greenantmall.R;
import com.greenantmall.bean.HotWords;
import com.greenantmall.service.RetrieveDataAsyn;
import com.greenantmall.service.RetrieveDataListener;
import com.greenantmall.utils.UtilTools;
import com.lsarah.utils.SystemBarTintManager;

public class SearchActivity extends BaseActivity {
	private Button mBtnBack;
	private Button mBtnClear;
	private EditText mEtSearch;
	private TextView mTvSearch;
	
	private View mViewHotSearchLayout;
	private TextView mTvHotWord1;
	private TextView mTvHotWord2;
	private TextView mTvHotWord3;
	private TextView mTvHotWord4;
	private TextView mTvHotWord5;
	private TextView mTvHotWord6;
	private TextView mTvHotWord7;
	private TextView mTvHotWord8;
	private TextView mTvHotWord9;
	private TextView mTvHotWord10;
	private TextView mTvHotWord11;
	
	private View mViewSearchHistoryLayout;
	private View mViewHistoryItem0;
	private View mViewHistoryItem1;
	private View mViewHistoryItem2;
	private View mViewHistoryItem3;
	private View mViewHistoryItem4;
	private TextView mTvHistoryWord0;
	private TextView mTvHistoryWord1;
	private TextView mTvHistoryWord2;
	private TextView mTvHistoryWord3;
	private TextView mTvHistoryWord4;
	private TextView mTvHistoryClear;
	private View mViewHistoryLine0;
	private View mViewHistoryLine1;
	private View mViewHistoryLine2;
	private View mViewHistoryLine3;
	private View mViewHistoryLine4;
	
	private OnClickListener mOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if(v.equals(mBtnBack)) {
				SearchActivity.this.finish();
			}
			else if(v.equals(mBtnClear)) {
				mEtSearch.setText("");
			}
			else if(v.equals(mTvHistoryClear)) {
				UtilTools.clearSearchHistory(getApplication());
				getSearchHistory();
			}
			else if(v.equals(mTvSearch)) {
				String keyWord = mEtSearch.getText().toString();
				goToSearchActivity2(keyWord);
			}
			else if(v.equals(mTvHotWord1)) {
				String keyWord = mTvHotWord1.getText().toString();
				goToSearchActivity2(keyWord);
			}
			else if(v.equals(mTvHotWord2)) {
				String keyWord = mTvHotWord2.getText().toString();
				goToSearchActivity2(keyWord);
			}
			else if(v.equals(mTvHotWord3)) {
				String keyWord = mTvHotWord3.getText().toString();
				goToSearchActivity2(keyWord);
			}
			else if(v.equals(mTvHotWord4)) {
				String keyWord = mTvHotWord4.getText().toString();
				goToSearchActivity2(keyWord);
			}
			else if(v.equals(mTvHotWord5)) {
				String keyWord = mTvHotWord5.getText().toString();
				goToSearchActivity2(keyWord);
			}
			else if(v.equals(mTvHotWord6)) {
				String keyWord = mTvHotWord6.getText().toString();
				goToSearchActivity2(keyWord);
			}
			else if(v.equals(mTvHotWord7)) {
				String keyWord = mTvHotWord7.getText().toString();
				goToSearchActivity2(keyWord);
			}
			else if(v.equals(mTvHotWord8)) {
				String keyWord = mTvHotWord8.getText().toString();
				goToSearchActivity2(keyWord);
			}
			else if(v.equals(mTvHotWord9)) {
				String keyWord = mTvHotWord9.getText().toString();
				goToSearchActivity2(keyWord);
			}
			else if(v.equals(mTvHotWord10)) {
				String keyWord = mTvHotWord10.getText().toString();
				goToSearchActivity2(keyWord);
			}
			else if(v.equals(mTvHotWord11)) {
				String keyWord = mTvHotWord11.getText().toString();
				goToSearchActivity2(keyWord);
			}
			else if(v.equals(mViewHistoryItem0)) {
				String keyWord = mTvHistoryWord0.getText().toString();
				goToSearchActivity2(keyWord);
			}
			else if(v.equals(mViewHistoryItem1)) {
				String keyWord = mTvHistoryWord1.getText().toString();
				goToSearchActivity2(keyWord);
			}
			else if(v.equals(mViewHistoryItem2)) {
				String keyWord = mTvHistoryWord2.getText().toString();
				goToSearchActivity2(keyWord);
			}
			else if(v.equals(mViewHistoryItem3)) {
				String keyWord = mTvHistoryWord3.getText().toString();
				goToSearchActivity2(keyWord);
			}
			else if(v.equals(mViewHistoryItem4)) {
				String keyWord = mTvHistoryWord4.getText().toString();
				goToSearchActivity2(keyWord);
			}
		}
	};
	
	private void goToSearchActivity2(String keyWord) {
		if(!TextUtils.isEmpty(keyWord)) {
			mEtSearch.setText(keyWord);
			Intent intent = new Intent(SearchActivity.this, SearchActivity2.class);
			intent.putExtra(SearchActivity2.KeySearchContent, keyWord);
			startActivity(intent);
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SystemBarTintManager.setContentView(this, R.layout.search_activity, getResources().getColor(R.color.green));
		
		mBtnBack = (Button)findViewById(R.id.search_activity_top_arrow_left);
		mBtnClear = (Button)findViewById(R.id.search_activity_cancel_icon);
		mEtSearch = (EditText)findViewById(R.id.search_activity_et);
		mTvSearch = (TextView)findViewById(R.id.search_activity_top_right_tv);
		mViewHotSearchLayout = findViewById(R.id.search_activity_hot_search_layout);
		mTvHotWord1 = (TextView)findViewById(R.id.search_activity_hot_search_tv_1);
		mTvHotWord2 = (TextView)findViewById(R.id.search_activity_hot_search_tv_2);
		mTvHotWord3 = (TextView)findViewById(R.id.search_activity_hot_search_tv_3);
		mTvHotWord4 = (TextView)findViewById(R.id.search_activity_hot_search_tv_4);
		mTvHotWord5 = (TextView)findViewById(R.id.search_activity_hot_search_tv_5);
		mTvHotWord6 = (TextView)findViewById(R.id.search_activity_hot_search_tv_6);
		mTvHotWord7 = (TextView)findViewById(R.id.search_activity_hot_search_tv_7);
		mTvHotWord8 = (TextView)findViewById(R.id.search_activity_hot_search_tv_8);
		mTvHotWord9 = (TextView)findViewById(R.id.search_activity_hot_search_tv_9);
		mTvHotWord10 = (TextView)findViewById(R.id.search_activity_hot_search_tv_10);
		mTvHotWord11 = (TextView)findViewById(R.id.search_activity_hot_search_tv_11);
		mViewSearchHistoryLayout = findViewById(R.id.search_activity_search_history_layout);
		mViewHistoryItem0 = findViewById(R.id.search_activity_history_ll_0);
		mViewHistoryItem1 = findViewById(R.id.search_activity_history_ll_1);
		mViewHistoryItem2 = findViewById(R.id.search_activity_history_ll_2);
		mViewHistoryItem3 = findViewById(R.id.search_activity_history_ll_3);
		mViewHistoryItem4 = findViewById(R.id.search_activity_history_ll_4);
		mTvHistoryWord0 = (TextView)findViewById(R.id.search_activity_history_tv_0);
		mTvHistoryWord1 = (TextView)findViewById(R.id.search_activity_history_tv_1);
		mTvHistoryWord2 = (TextView)findViewById(R.id.search_activity_history_tv_2);
		mTvHistoryWord3 = (TextView)findViewById(R.id.search_activity_history_tv_3);
		mTvHistoryWord4 = (TextView)findViewById(R.id.search_activity_history_tv_4);
		mViewHistoryLine0 = findViewById(R.id.search_activity_history_line_0);
		mViewHistoryLine1 = findViewById(R.id.search_activity_history_line_1);
		mViewHistoryLine2 = findViewById(R.id.search_activity_history_line_2);
		mViewHistoryLine3 = findViewById(R.id.search_activity_history_line_3);
		mViewHistoryLine4 = findViewById(R.id.search_activity_history_line_4);
		mTvHistoryClear = (TextView)findViewById(R.id.search_activity_btn_clear_history);
		
		mBtnBack.setOnClickListener(mOnClickListener);
		mBtnClear.setOnClickListener(mOnClickListener);
		mTvSearch.setOnClickListener(mOnClickListener);
		mTvHotWord1.setOnClickListener(mOnClickListener);
		mTvHotWord2.setOnClickListener(mOnClickListener);
		mTvHotWord3.setOnClickListener(mOnClickListener);
		mTvHotWord4.setOnClickListener(mOnClickListener);
		mTvHotWord5.setOnClickListener(mOnClickListener);
		mTvHotWord6.setOnClickListener(mOnClickListener);
		mTvHotWord7.setOnClickListener(mOnClickListener);
		mTvHotWord8.setOnClickListener(mOnClickListener);
		mTvHotWord9.setOnClickListener(mOnClickListener);
		mTvHotWord10.setOnClickListener(mOnClickListener);
		mTvHotWord11.setOnClickListener(mOnClickListener);
		mViewHistoryItem0.setOnClickListener(mOnClickListener);
		mViewHistoryItem1.setOnClickListener(mOnClickListener);
		mViewHistoryItem2.setOnClickListener(mOnClickListener);
		mViewHistoryItem3.setOnClickListener(mOnClickListener);
		mViewHistoryItem4.setOnClickListener(mOnClickListener);
		mTvHistoryClear.setOnClickListener(mOnClickListener);
		
		mViewHotSearchLayout.setVisibility(View.GONE);
		mViewHistoryItem0.setVisibility(View.GONE);
		mViewHistoryItem1.setVisibility(View.GONE);
		mViewHistoryItem2.setVisibility(View.GONE);
		mViewHistoryItem3.setVisibility(View.GONE);
		mViewHistoryItem4.setVisibility(View.GONE);
		mViewHistoryLine0.setVisibility(View.GONE);
		mViewHistoryLine1.setVisibility(View.GONE);
		mViewHistoryLine2.setVisibility(View.GONE);
		mViewHistoryLine3.setVisibility(View.GONE);
		mViewHistoryLine4.setVisibility(View.GONE);
		mTvHistoryClear.setVisibility(View.GONE);
		
		this.getSearchHotWords();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		this.getSearchHistory();
	}
	
	private void getSearchHotWords() {
		RetrieveDataAsyn.getInstance().getHotWords(-1, new RetrieveDataListener() {
			@Override
			public void dataRetrieved(int requestId, Object data) {
				if(data != null) {
					List<HotWords> hotWordsList = (List<HotWords>)data;
					int size = hotWordsList.size();
					Log.d("CEXX", "SearchActivity ---> getSearchHotWords ---> hotWordsList.size :　" + size);
					if(size > 0) {
						mViewHotSearchLayout.setVisibility(View.VISIBLE);
						for(int i=0; i<size; i++) {
							String hotWords = hotWordsList.get(i).getWords();
							switch(i)
							{
							case 0:
								mTvHotWord1.setText(hotWords);
								break;
							case 1:
								mTvHotWord2.setText(hotWords);
								break;
							case 2:
								mTvHotWord3.setText(hotWords);
								break;
							case 3:
								mTvHotWord4.setText(hotWords);
								break;
							case 4:
								mTvHotWord5.setText(hotWords);
								break;
							case 5:
								mTvHotWord6.setText(hotWords);
								break;
							case 6:
								mTvHotWord7.setText(hotWords);
								break;
							case 7:
								mTvHotWord8.setText(hotWords);
								break;
							case 8:
								mTvHotWord9.setText(hotWords);
								break;
							case 9:
								mTvHotWord10.setText(hotWords);
								break;
							case 10:
								mTvHotWord11.setText(hotWords);
								break;
							}
						}
					}
				}
			}
		});
	}
	
	private void getSearchHistory() {
		mViewHistoryItem0.setVisibility(View.GONE);
		mViewHistoryItem1.setVisibility(View.GONE);
		mViewHistoryItem2.setVisibility(View.GONE);
		mViewHistoryItem3.setVisibility(View.GONE);
		mViewHistoryItem4.setVisibility(View.GONE);
		mViewHistoryLine0.setVisibility(View.GONE);
		mViewHistoryLine1.setVisibility(View.GONE);
		mViewHistoryLine2.setVisibility(View.GONE);
		mViewHistoryLine3.setVisibility(View.GONE);
		mViewHistoryLine4.setVisibility(View.GONE);
		mTvHistoryClear.setVisibility(View.GONE);
		
		List<String> historyList = UtilTools.getSearchHistory(getApplication());
		if(historyList != null && historyList.size() > 0) {
			int size = historyList.size();
			Log.d("CEXX", "SearchActivity ---> getSearchHistory ---> historyList.size : " + size);
			if(size > 0) {
				mViewHistoryItem4.setVisibility(View.VISIBLE);
				mViewHistoryLine4.setVisibility(View.VISIBLE);
				mTvHistoryWord4.setText(historyList.get(size-1));
			}
			
			if(size > 1) {
				mViewHistoryItem3.setVisibility(View.VISIBLE);
				mViewHistoryLine3.setVisibility(View.VISIBLE);
				mTvHistoryWord3.setText(historyList.get(size-2));
			}
			
			if(size > 2) {
				mViewHistoryItem2.setVisibility(View.VISIBLE);
				mViewHistoryLine2.setVisibility(View.VISIBLE);
				mTvHistoryWord2.setText(historyList.get(size-3));
			}
			
			if(size > 3) {
				mViewHistoryItem1.setVisibility(View.VISIBLE);
				mViewHistoryLine1.setVisibility(View.VISIBLE);
				mTvHistoryWord1.setText(historyList.get(size-4));
			}
			
			if(size > 4) {
				mViewHistoryItem0.setVisibility(View.VISIBLE);
				mViewHistoryLine0.setVisibility(View.VISIBLE);
				mTvHistoryWord0.setText(historyList.get(size-5));
			}
			
			mTvHistoryClear.setVisibility(View.VISIBLE);
		}
	}
}













